package com.example.bilibili.home;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.bilibili.Constants;
import com.example.bilibili.GoodsBean;
import com.example.bilibili.R;
import com.example.bilibili.ResultBeanData;
import com.example.bilibili.utils.MediaView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class FragmentA extends Fragment {

    private Context mContext;

    private ImageView touxiang;
    private TextView fensi;
    private TextView id;
    private TextView bofangliang;
    private TextView pinglun;
    private TextView shijian;
    private TextView yingbi;
    private CheckBox dianzan;
    private CheckBox toubi;
    private CheckBox shoucang;
    private CheckBox zhuanfa;
    private CheckBox cai;

    private String Stouxiang;
    private String Sid;
    private String Sfensi;
    private String Sbofangliang;
    private String Spinglun;
    private String Sshijian;
    private String Sdianzan;
    private String Stoubi;
    private String Sshoucang;
    private String Szhuanfa;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            mContext=getActivity();
            View view = inflater.inflate(R.layout.fragment_jianjie,container,false);
            touxiang = (ImageView)view.findViewById( R.id.touxiang );
            fensi = (TextView)view.findViewById( R.id.fensi );
            id = (TextView)view.findViewById( R.id.id );
            yingbi = (TextView)view.findViewById( R.id.yingbi );
            bofangliang = (TextView)view.findViewById( R.id.bofangliang );
            pinglun = (TextView)view.findViewById( R.id.pinglun );
            shijian = (TextView)view.findViewById( R.id.shijian );
            dianzan = (CheckBox)view.findViewById( R.id.dianzan );
            toubi = (CheckBox)view.findViewById( R.id.toubi );
            shoucang = (CheckBox)view.findViewById( R.id.shoucang );
            zhuanfa = (CheckBox)view.findViewById( R.id.zhuanfa);
            cai = (CheckBox)view.findViewById( R.id.cai);


            Bundle bundlea =this.getArguments();//得到从Activity传来的数据
            Stouxiang = null;
            Sid = null;
            Sfensi = null;
            Sbofangliang = null;
            Spinglun = null;
            Sshijian = null;
            Sdianzan = null;
            Stoubi = null;
            Sshoucang = null;
            Szhuanfa = null;
            if(bundlea!=null){
                Stouxiang = bundlea.getString("touxiang");
                Sid = bundlea.getString("id");
                Sfensi = bundlea.getString("fensi");
                Sbofangliang = bundlea.getString("bofangliang");
                Spinglun = bundlea.getString("pinglun");
                Sshijian = bundlea.getString("shijian");
                Sdianzan = bundlea.getString("dianzan");
                Stoubi = bundlea.getString("toubi");
                Sshoucang = bundlea.getString("shoucang");
                Szhuanfa = bundlea.getString("zhuanfa");
                Glide.with(mContext).load(Stouxiang).into(touxiang);
                id.setText(Sid);
                fensi.setText(Sfensi);
                bofangliang.setText(Sbofangliang);
                pinglun.setText(Spinglun);
                shijian.setText(Sshijian);
                dianzan.setText(Sdianzan);
                toubi.setText(Stoubi);
                shoucang.setText(Sshoucang);
                zhuanfa.setText(Szhuanfa);

            }

        //点赞
        dianzan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    dianzan.setText(exchangejia(Sdianzan));
                    cai.setChecked(false);
                }else{
                    dianzan.setText(Sdianzan);
                }
            }
        });

        //踩
        cai.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    dianzan.setText(exchangejian(Sdianzan));
                    dianzan.setChecked(false);
                }else{
                    dianzan.setText(Sdianzan);
                }
            }
        });

        //投币
        toubi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    toubi.setText(exchangejia(Stoubi));
                    dianzan.setChecked(true);
                }else{
                    toubi.setText(Stoubi);
                }
            }
        });

        //收藏
        shoucang.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    shoucang.setText(exchangejia(Sshoucang));
                }else{
                    shoucang.setText(Sshoucang);
                }
            }
        });

        //转发
        zhuanfa.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    zhuanfa.setText(exchangejia(Szhuanfa));
                }else{
                    zhuanfa.setText(Szhuanfa);
                }
            }
        });

            return view;
        }

    private String exchangejian(String string) {

        if(string.contains("万")){
            string = string;
        }else{
            try {
                int intstring = Integer.parseInt(string);
                intstring = intstring-1;
                string = intstring+"";
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }

        return string;
    }

    private String exchangejia(String string) {

        if(string.contains("万")){
            string = string;
        }else{
            try {
                int intstring = Integer.parseInt(string);
                intstring = intstring+1;
                string = intstring+"";
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }

        return string;
    }
}

